package com.taptech.doufu.ui.adapter.personalcenter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.adapter.BaseListAdapter;
import com.taptech.doufu.bean.personalcenter.WeiboUserBean;
import com.taptech.doufu.ui.view.NetworkImageView;

public class WeiboUserAdapter extends BaseListAdapter {

	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;

		if (convertView == null) {
			convertView =  LayoutInflater.from(parent.getContext()).inflate(
					R.layout.personal_center_adapter_weibo_user, null);
			holder = new ViewHolder();
			holder.userHeader = (NetworkImageView) convertView
					.findViewById(R.id.personal_center_weibo_user_icon);
			holder.userHeader.setFullScreen(false);
			holder.userName = (TextView) convertView
					.findViewById(R.id.personal_center_weibo_user_name);
			holder.selectBtn = (ImageView) convertView
					.findViewById(R.id.personal_center_weibo_user_checkBox);
			convertView.setTag(holder);
		} else
			holder = (ViewHolder) convertView.getTag();
		initData(convertView, position);
		return convertView;
	}

	private void initData(View convertView, final int position) {
		final WeiboUserBean weiboUser = (WeiboUserBean) getItem(position);
		final ViewHolder holder = (ViewHolder) convertView.getTag();
		if (weiboUser.isSelected())
			holder.selectBtn.setImageResource(R.drawable.check_selected);
		else
			holder.selectBtn.setImageResource(R.drawable.check_dis_selected);
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (weiboUser.isSelected()) {
					holder.selectBtn
							.setImageResource(R.drawable.check_dis_selected);
					weiboUser.setSelected(false);
				} else {
					holder.selectBtn
							.setImageResource(R.drawable.check_selected);
					weiboUser.setSelected(true);
				}
			}
		});

		holder.userHeader.setURL(weiboUser.getProfile_image_url());
		holder.userName.setText(weiboUser.getScreen_name());
	}

	class ViewHolder {
		NetworkImageView userHeader;
		TextView userName;
		ImageView selectBtn;
	}
}
